package com.xebest.web.controller.restapi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.xebest.api.dto.*;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.dto.BaseResponseDTO;
import com.xebest.common.base.pagedto.XePageInfoDto;
import com.xebest.common.enums.erp.finance.VenPrepayStatusEnum;
import com.xebest.constant.XeErrorCode;
import com.xebest.web.controller.finance.XeVenFinPrepayBillController;
import com.xebest.web.form.XeVenFinPrepayBillQueryForm;
import com.xebest.web.model.XeVenFinPrepaySettle;
import com.xebest.web.service.XeVenFinPrepayBillService;
import com.xebest.web.view.XeVenFinPrepayBillView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * 应付单对外接口
 *
 * @author 杜春静
 */
@RestController
@RequestMapping("restapi/finPrepay")
public class XeVenFinPrepayBillRestApi extends BaseController {


    private static final Logger log = LoggerFactory.getLogger(XeVenFinPrepayBillController.class);// 日志记录类

    @Autowired
    XeVenFinPrepayBillService prepayBillService;

    /**
     * 根据供货商查询预付完成的预付单数据
     *
     * @author 杜春静
     */
    @RequestMapping("/queryPrepayFinishBill")
    public XeVenFinPrepayBillResponseDTO queryPrepayFinishBill(@RequestBody XeVenFinPrepayBillQueryDTO formDTO) {
        XeVenFinPrepayBillResponseDTO pbResponseDTO = new XeVenFinPrepayBillResponseDTO();
        pbResponseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        List<XeVenFinPrepayBillViewDTO> prepayList = new ArrayList<XeVenFinPrepayBillViewDTO>();
        String paramJson = JSONObject.toJSONString(formDTO);
        log.info("查询预付完成的预付单请求参数为：{}" + paramJson);

        //验证参数
        if (null == formDTO || StringUtils.isEmpty(formDTO.getSupplierCode())) {
            pbResponseDTO.setStatus(XeErrorCode.PORJECT_PARAM_ERROR);
            return pbResponseDTO;
        }
        try {
            XeVenFinPrepayBillQueryForm form = JSONObject.parseObject(paramJson, XeVenFinPrepayBillQueryForm.class);

            form.setPrepayStatus(VenPrepayStatusEnum.PREPAY_FINISH.getValue());
            form.setIsRelationSettle("YES");
            //封装分页查询数据
            XePageInfoDto xePageInfo;
            PageHelper.startPage(form.getPageNum(), form.getPageSize(), true);
            //查询预付单列表
            List<XeVenFinPrepayBillView> prepayBill = prepayBillService.queryPrepayBillList(null, form);
            if (prepayBill != null && prepayBill.size() > 0) {
                String detailListStr = JSONArray.toJSONString(prepayBill);
                prepayList = JSONArray.parseArray(detailListStr, XeVenFinPrepayBillViewDTO.class);
                pbResponseDTO.setStatus(XeErrorCode.PORJECT_SUCCESS);
            }
            xePageInfo = new XePageInfoDto<>(prepayBill);
            pbResponseDTO.setPrepayBillList(prepayList);
            pbResponseDTO.setXePageInfoDto(xePageInfo);

        } catch (Exception e) {
            log.error("查询预付完成的预付单异常{}", e);
        }
        return pbResponseDTO;
    }

    /**
     * 结账单关联预付单
     *
     * @author 杜春静
     */
    @RequestMapping("/addRelationSettlePrepay")
    public BaseResponseDTO addRelationSettlePrepay(@RequestBody XeVenFinPrepayBillRequestDTO pbRequestDTO) {
        String detailListStr = JSONArray.toJSONString(pbRequestDTO);
        log.info("添加预付单和结账单关联关系请求参数为：{}" + detailListStr);
        List<XeVenFinPrepaySettleRequestDTO> settleRequestList = pbRequestDTO.getSettleList();
        if (settleRequestList == null || settleRequestList.size() <= 0) {
            return sendApiError(pbRequestDTO.getSpanId());
        }
        try {
            List<XeVenFinPrepaySettle> spList = new ArrayList<XeVenFinPrepaySettle>();
            String settleListStr = JSONArray.toJSONString(settleRequestList);
            spList = JSONArray.parseArray(settleListStr, XeVenFinPrepaySettle.class);
            int result = prepayBillService.addRelationSettlePrepay(spList);
            if (result > 0) {
                return sendApiOK(pbRequestDTO.getSpanId());
            }
        } catch (Exception e) {
            log.info("添加预付单和结账单关联关系失败{}", e);
        }
        return sendApiError(pbRequestDTO.getSpanId());

    }

    /**
     * 解除结账单和预付单的关联关系
     *
     * @author 杜春静
     */
    @RequestMapping("/relieveRelationSettlePrepay")
    public BaseResponseDTO relieveRelationSettlePrepay(@RequestBody XeVenFinPrepayBillRequestDTO pbRequestDTO) {
        String detailListStr = JSONArray.toJSONString(pbRequestDTO);
        log.info("解除预付单和结账单关联关系请求参数为：{}" + detailListStr);
        if (pbRequestDTO == null || StringUtils.isEmpty(pbRequestDTO.getModifyUser())
                || pbRequestDTO.getSatementNos() == null || pbRequestDTO.getSatementNos().length <= 0) {
            return sendApiError(pbRequestDTO.getSpanId());
        }
        try {
            int result = prepayBillService.relieveRelationSettlePrepay(pbRequestDTO.getModifyUser(), pbRequestDTO.getSatementNos());
            if (result > 0) {
                return sendApiOK(pbRequestDTO.getSpanId());
            }
        } catch (Exception e) {
            log.info("解除预付单和结账单关联关系失败");
        }
        return sendApiError(pbRequestDTO.getSpanId());
    }


}
  
